﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using WebTest.Core.Entities.OrderAggregate;

namespace WebTest.Core.Entities
{
    public class Member : BaseEntity
    {
        /// <summary>
        /// 会员账号
        /// </summary>
        public string Account { get; private set; }
        /// <summary>
        /// 会员名(别名)
        /// </summary>
        public string MemberName { get; private set; }
        /// <summary>
        /// 会员密码
        /// </summary>
        public string Password { get; private set; }
        private readonly List<Address> _address = new List<Address>();
        /// <summary>
        /// 会员所有的地址
        /// </summary>
        public IEnumerable<Address> MemberAddress
        {
            get
            {
                return _address;
            }
        }
        private readonly List<Role> _roles = new List<Role>();
        /// <summary>
        /// 会员所属角色
        /// </summary>
        public IEnumerable<Role> Roles
        {
            get
            {
                return _roles;
            }
        }
        private readonly List<Order> _orders = new List<Order>();
        public IEnumerable<Order> Orders
        {
            get
            {
                return _orders;
            }
        }
        public Member(string account, string memberName = null)
        {
            Account = account ?? throw new ArgumentNullException(nameof(account));
            MemberName = memberName;
        }
        public static string CreateHashPassword(string password)
        {
            using (var md5 = MD5.Create())
            {
                var buffer = Encoding.UTF32.GetBytes(password);
                var hash = md5.ComputeHash(buffer);
                return BitConverter.ToString(hash).Replace("-", "");
            }
        }
        /// <summary>
        /// 创建Md5密码
        /// </summary>
        /// <param name="password"></param>
        public void SetPassword(string password)
        {
            Password = CreateHashPassword(password);
        }
        /// <summary>
        /// 修改会员名(别名)
        /// </summary>
        /// <param name="name"></param>
        public void SetMemberName(string name)
        {
            MemberName = name;
        }
        /// <summary>
        /// 添加会员地址
        /// </summary>
        /// <param name="address"></param>
        public void AddMemberAddress(Address address)
        {
            if (!_address.Exists(x => x.Alias == address.Alias && x.Detail == address.Detail))
            {
                _address.Add(address);
            }
        }
        /// <summary>
        /// 删除会员地址
        /// </summary>
        /// <param name="address"></param>
        public void RemoveMemberAddress(Address address)
        {
            if(_address.Exists(x=>x.Alias == address.Alias && x.Detail == address.Detail))
            {
                _address.Remove(address);
            }
        }
        /// <summary>
        /// 清空会员地址
        /// </summary>
        public void ClearMemberAddress()
        {
            if (_address.Any())
            {
                _address.Clear();
            }
        }
        public void CreateOrder(Order order)
        {
            if(!_orders.Exists(x => x.Id != order.Id))
            {
                _orders.Add(order);
            }
        }
    }
}
